package tetris;

import java.awt.*;
import java.lang.Override;
import java.lang.String;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: Slava
 * Date: 17.05.12
 * Time: 16:09
 * To change this template use File | Settings | File Templates.
 */
public class StringBoard implements Board<String> {

    private int rows;
    private int columns;
    private static final char FREE_BLOCK = '*';
    private static final char Y = 'Y';
    private final char[][] gameBoard;
    private boolean isFalling;

    private Dimension fallingBlock;

    public StringBoard(int rows, int columns) {
        if (columns < 2 || rows < 2) {
            throw new IllegalArgumentException("Achtung!");
        }
        this.columns = columns;
        this.rows = rows;
        this.gameBoard = new char[rows][columns];
        for (int i = 0; i < rows; i++) {
            Arrays.fill(gameBoard[i], FREE_BLOCK);
        }
    }

    @Override
    public String render() {
        StringBuilder boardBuilder = new StringBuilder();
        for (int i = 0; i < gameBoard.length; i++) {
            for (int j = 0; j < gameBoard[i].length; j++) {
                boardBuilder.append(gameBoard[i][j]);
            }
            if (i < gameBoard.length - 1) {
                boardBuilder.append("\n");
            }
        }
        return boardBuilder.toString();
    }

    @Override
    public boolean fallingBlocks() {
        return isFalling;
    }


    @Override
    public void startGame() {
        int fallingColumn = columns % 2 == 0 ? columns / 2 - 1 : columns / 2;
        fallingBlock = new Dimension(0, fallingColumn);
        updateGameBoard(fallingBlock);
    }

//<<<<<<< .mine
//    public void updateGameBoard(Dimension fallingBlock) {
//        gameBoard[(int) fallingBlock.getWidth()][(int) fallingBlock.getHeight() - 1] = FREE_BLOCK;
//        gameBoard[(int) fallingBlock.getWidth()][(int) fallingBlock.getHeight()] = Y;
//=======
    public void updateGameBoard(Dimension fallingBlock) {
        if (fallingBlock.getWidth() > 0) {
            gameBoard[(int) fallingBlock.getWidth() - 1][(int) fallingBlock.getHeight()] = FREE_BLOCK;
        }
        gameBoard[(int) fallingBlock.getWidth()][(int) fallingBlock.getHeight()] = Y;

    }


    @Override
    public void tick() {
        fallingBlock.setSize(fallingBlock.getWidth() + 1, fallingBlock.getHeight());
        updateGameBoard(fallingBlock);
    }


    @Override
    public void moveRight() {
        //if its maximum width dont move
        if (this.fallingBlock.getWidth() < this.rows - 1 && gameBoard[this.fallingBlock.getWidth() + 1][this.fallingBlock.getHeight()] != Y) {
            gameBoard[this.fallingBlock.getWidth()][this.fallingBlock.getHeight()] = FREE_BLOCK;
            gameBoard[this.fallingBlock.getWidth() + 1][this.fallingBlock.getHeight()] = Y;
        }
    }

    @Override
    public boolean isFinished() {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }



    @Override
    public void moveLeft() {
        //if its maximum width dont move
        if (this.fallingBlock.getWidth() > 0 && gameBoard[this.fallingBlock.getWidth() - 1][this.fallingBlock.getHeight()] != Y) {
            gameBoard[this.fallingBlock.getWidth()][this.fallingBlock.getHeight()] = FREE_BLOCK;
            gameBoard[this.fallingBlock.getWidth() - 1][this.fallingBlock.getHeight()] = Y;
        }
    }

    /**
     * get user input from keyboard
     * @athour Dan & Alex
     */
    public void readUserInput () {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String action = null;
        try {
            action = br.readLine();
            handleUserInput(action);
        } catch (IOException e) {
            System.out.println("Error!");
            System.exit(1);
        }
    }

    /**
     * moving left or right if arrows pressed
     * @athour Dan & Alex
     */
    public void handleUserInput(String input) {
        if (input == '0x2190') {
            moveLeft();    
        }
        else if(input == '0x2192') {
            moveRight();
        }
    }

}
